package com.cy.dda.web.admin.collection;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.comm.utils.ExportExcelUtils;
import com.cy.dda.model.PageBean;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.collection.RepayRecordEditAO;
import com.cy.dda.model.domain.collection.RepayRecord;
import com.cy.dda.model.enums.collection.RepayStatus;
import com.cy.dda.model.query.collection.RepayRecordListAdminQuery;
import com.cy.dda.model.vo.collection.CollectionRecordDetailInfoAdminVO;
import com.cy.dda.model.vo.collection.RepayRecordListAdminVO;
import com.cy.dda.service.collection.RepayRecordService;
import com.cy.dda.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-催收回款记录管理
 *
 * @author: tanyilin
 * @create: 2018年11月6日 上午11:10:15
 */
@Slf4j
@RestController
@RequestMapping("/admin/repay/record")
@Api(tags = "后台-回款记录管理")
public class AdminRepayRecordController extends BaseController {

	@Autowired
	private RepayRecordService repayRecordService;
	
	@ApiOperation("回款记录列表")
	@GetMapping("/list")
	public PageBean<RepayRecordListAdminVO> selectRepayRecordListAdmin(RepayRecordListAdminQuery query) {
		PageBean<RepayRecordListAdminVO> pageBean = repayRecordService.selectRepayRecordListAdmin(query);
		return pageBean;
	}
	
	@ApiOperation("确认操作")
	@PostMapping("/do/confirm")
	public Result doConfirm(@Validated RepayRecordEditAO editAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		RepayRecord repayRecord = new RepayRecord();
		BeanUtils.copyProperties(editAO, repayRecord);
		repayRecord.setId(editAO.getRepayRecordId());
		repayRecord.setRepayStatus(RepayStatus.CONFIRMED);
		repayRecord.setUpdateTime(new Date());
		repayRecordService.updateRepayRecord(repayRecord);
		return ok();
	}
	
	@ApiOperation("取消操作")
	@PostMapping("/do/cancel")
	public Result doCancel(@Validated RepayRecordEditAO editAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		RepayRecord repayRecord = new RepayRecord();
		repayRecord.setId(editAO.getRepayRecordId());
		repayRecord.setRepayStatus(RepayStatus.CANCELED);
		repayRecord.setUpdateTime(new Date());
		repayRecordService.updateRepayRecord(repayRecord);
		return ok();
	}
	
	@ApiOperation("回款记录详情信息")
	@GetMapping("/get/detail/info")
	public Result<CollectionRecordDetailInfoAdminVO> getDetailInfo(@RequestParam Long repayRecordId) {
		CollectionRecordDetailInfoAdminVO modelVo = repayRecordService.selectRepayRecordDetailInfoAdminVO(repayRecordId);
		return ok("查询成功", modelVo);
	}
	
	@ApiOperation("导出列表")
	@GetMapping("/list/export")
	public void listExport(RepayRecordListAdminQuery query, HttpServletRequest req, HttpServletResponse resp) {
		query.setPage(1);
		query.setLimit(5000);
		List<RepayRecordListAdminVO> data = this.selectRepayRecordListAdmin(query).getData();
		String title = "回款记录列表";
		String[] headers = {"回款时间", "委案机构", "委案类型", "委案状态", "客户", "逾期期数", "回款情况", "回款金额", "回款方式", "催收员", "回款备注", "状态"};
		String[] fieldNames = {"createTime", "orgName", "casesTypeText", "casesStatusText", "customerName", "overduePeriod", "repayTypeText", "amount", "repayWayText", 
				"collectionUserName", "repayRemark", "repayStatusText"};
		ExportExcelUtils<RepayRecordListAdminVO> ex = new ExportExcelUtils<>();
		try(OutputStream out = resp.getOutputStream()) {
			req.setCharacterEncoding("UTF-8");
			resp.setCharacterEncoding("UTF-8");
			resp.setContentType("application/x-download");
			String fileName = CommonUtils.getCurrentTimeAndRandomNumber().concat(".xls");
			fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
			resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			ex.exportExcel(title, headers, fieldNames, data, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
